package gooner.suntine.ssh.eg.action;

import gooner.suntine.ssh.eg.entity.Student;
import gooner.suntine.ssh.eg.service.StudentService;
import gooner.suntine.ssh.eg.util.ApplicationContextUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;


/**
 * @corporation 
 * @author suntine
 * @creation 2011-5-5 09:53:47 AM
 * @description 
 */
public class StudentAction {
	
	//使用注解注入Service,也可使用@Autowired
	@Resource(name="studentService")
	private StudentService studentService = (StudentService)ApplicationContextUtils.getBean("studentService");
	//也可以用自定义的类ApplicationContextUtils取
	//private StudentService studentService = (StudentService)ApplicationContextUtils.getBean("studentService");
	private List<Student> list = new ArrayList<Student>();
	private Student student;
	
	private HttpServletRequest request ;
	private HttpServletResponse response ;
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	public String page(){
		logger.info("用户进入首页");
		return "success";
	}
	
	public void search() throws IOException{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		
		String ajaxResponseType = "json";
		
		list = studentService.searchStudent("from Student");
		JSONArray jsonArray = JSONArray.fromObject(list);
		
	    response.setCharacterEncoding("UTF-8");//这句不能注释掉，即便配了struts的I18N属性
	    if ("json".equals(ajaxResponseType))
	      response.setContentType("application/json");
	    else if ("xml".equals(ajaxResponseType))
	      response.setContentType("text/xml");
	    else {
	      response.setContentType("text/plain");
	    }
	    
	    PrintWriter out = response.getWriter();
	    out.write((jsonArray == null) ? "" : jsonArray.toString());
	    out.flush();
	    out.close();
	    
	    logger.info("用户查询学员");
	    
	}
	
	public void add() throws IOException {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		
//		int min = 100000;
//		int max = 999999;
//		Random   rand   =   new   Random(); 
//		int id = rand.nextInt(max)%(max-min+1) + min;
//		student.setId(id);
		try {
			logger.info("用户增加学员");
			studentService.createStudent(student);
			response.getWriter().print("success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			response.getWriter().print("error");
		}
	}
	
	public void delete() throws IOException {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		
		String id = request.getParameter("id");
		try {
			logger.info("用户删除学员");
			studentService.deleteStudentById(Integer.parseInt(id));
			response.getWriter().print("success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			response.getWriter().print("error");
		}
	}
	
	public void update() throws IOException{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		
		try {
			logger.info("用户修改学员");
			studentService.updateStudent(student);
			response.getWriter().print("success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			response.getWriter().print("error");
		}
	}
	
	
	public void get() throws IOException{
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		
		String id = request.getParameter("id");
		logger.debug("该学员ID为："+id);
		
		Student s = studentService.getStudentById(Integer.parseInt(id));
		JSONObject jsonObj = JSONObject.fromObject(s);
		
	    response.setCharacterEncoding("UTF-8");
	    response.setContentType("application/json");
	    PrintWriter out = response.getWriter();
	    out.write((jsonObj == null) ? "" : jsonObj.toString());
	    out.flush();
	    out.close();
	    
	}
	
	public List<Student> getList() {
		return list;
	}
	public void setList(List<Student> list) {
		this.list = list;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}
	
}
